<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :search.sync="search"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" plain icon="el-icon-download" @click="handleExport">导出 </el-button>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
    </avue-crud>
    <export-excel
      :visible="exprotVisible"
      :option="option"
      :page="page"
      :data="exportData"
      name="工具事物导出"
      :loading="btnLoading"
      @selectData="handleSelectData"
      @close="handleClose"
    ></export-excel>
  </basic-container>
</template>

<script>
import { getSteelMeshList, getToolUsage } from '@/api/admin/equipment';
import exportExcel from '@/components/export-excel';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';

export default {
  components: {
    exportExcel,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'TOOL_TRANSACTION_RECORD',
      exprotVisible: false,
      btnLoading: false,
      exportData: [],
      form: {},
      query: {},
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      search: {}, //搜索条件
      selectionList: [],
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 4,
        searchMenuPosition: 'right',
        border: true,
        index: true,
        indexLabel: '序号',
        viewBtn: true,
        selection: false,
        dialogClickModal: false,
        addBtn: false, //新增按钮是否显示
        menu: false, //表格操作栏是否显示
        searchSpan: 4,
        columnBtn: false,
        column: [
          {
            label: '工具编码',
            prop: 'equipmentCode',
            search: true
          },
          {
            label: '工具名称',
            prop: 'equipmentName',
            search: true
          },
          {
            label: '规格',
            prop: 'equipmentSpecification'
          },
          {
            label: '产线',
            prop: 'lineName'
          },
          {
            label: '工单号',
            prop: 'workOrderNo',
            search: true
          },
          {
            label: '事务类型',
            prop: 'transTypeValue',
            search: true,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=tool_usage_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '操作人员',
            prop: 'operator'
          },
          {
            label: '操作时间',
            prop: 'updateTime'
          },
          {
            label: '使用次数',
            prop: 'timesUsedThis'
          }
        ]
      },
      data: []
    };
  },
  computed: {},
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.transactionTime && params.transactionTime.length > 0) {
        params.startTransactionDate = params.transactionTime[0];
        params.endTransactionDate = params.transactionTime[1];
        delete params.transactionTime;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      for (let key in this.query) {
        if (key != 'transTypeValue') {
          this.query[key + '_like'] = this.query[key];
          delete this.query[key];
        } else {
          this.query['transType'] = this.query[key];
          delete this.query[key];
        }
      }
      this.query.descs = 'create_time';
      getToolUsage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.exportData = data.records;
        this.loading = false;
      });
    },
    handleExport() {
      this.exportData = this.data;
      this.exprotVisible = true;
    },
    handleClose(val) {
      this.exprotVisible = false;
      this.data = this.exportData.splice(0, this.page.pageSize);
    },
    handleSelectData(val) {
      let current = 1;
      let pageSize = this.page.pageSize;
      if (val === 'all') {
        // 接口返回上限500条数据
        pageSize = 500;
      } else {
        current = this.page.currentPage;
      }
      this.btnLoading = true;
      getToolUsage(current, pageSize, Object.assign(this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.exportData = data.records;
        this.btnLoading = false;
      });
    }
  }
};
</script>

<style></style>
